﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (Request.QueryString["id"] != null)
        {
            if (Session["WelcomeUser"] != null)
            {
                if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("ADM"))
                {
                    if (!IsPostBack)
                    {
                        GenerateDates gd = new GenerateDates();

                        // Lấy toàn bộ năm đổ vào ddlYear
                        ddlYear.DataSource = gd.generateYear();
                        ddlYear.DataTextField = "Text";
                        ddlYear.DataValueField = "Value";
                        ddlYear.DataBind();

                        if (udm.createConnection() != null && udm.showDepartment(udm.createConnection()) != null)
                        {
                            ddlDepartment.DataSource = udm.showDepartment(udm.createConnection());
                            ddlDepartment.DataTextField = "Name";
                            ddlDepartment.DataValueField = "ID";
                            ddlDepartment.DataBind();

                            ddlPosition.DataSource = udm.showPosition(udm.createConnection());
                            ddlPosition.DataTextField = "Name";
                            ddlPosition.DataValueField = "ID";
                            ddlPosition.DataBind();
                        }
                        else
                        {
                            Response.Redirect("~/ErrorPage.aspx");
                        }

                        //Lay id truyen tu querry string
                        int id = Convert.ToInt32(Request.QueryString["id"].ToString());

                        // Load thong tin event co id tren vao form
                        DataSet ds = new DataSet();

                        ds = udm.showEmployeeByID(udm.createConnection(), id);

                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            string firstName = ds.Tables[0].Rows[0][1].ToString();
                            string lastName = ds.Tables[0].Rows[0][2].ToString();
                            string position = ds.Tables[0].Rows[0][4].ToString();
                            string department = ds.Tables[0].Rows[0][5].ToString();
                            DateTime dateOfBirth = Convert.ToDateTime(ds.Tables[0].Rows[0][6].ToString());
                            string street = ds.Tables[0].Rows[0][7].ToString();
                            string state = ds.Tables[0].Rows[0][8].ToString();
                            string city = ds.Tables[0].Rows[0][9].ToString();
                            string phone = ds.Tables[0].Rows[0][10].ToString();
                            string email = ds.Tables[0].Rows[0][11].ToString();
                            string description = ds.Tables[0].Rows[0][13].ToString();
                            bool gender = Convert.ToBoolean(ds.Tables[0].Rows[0][14].ToString());

                            // Luu link image len session de khi nguoi dung ko upload file moi thi dung luon link nay
                            Session["fileUpload"] = ds.Tables[0].Rows[0][12].ToString();
                            Session["email"] = email;

                            // Bat dau do vao form
                            txtFirstName.Text = firstName;
                            txtLastName.Text = lastName;

                            ddlPosition.SelectedValue = position;
                            ddlDepartment.SelectedValue = department;

                            ddlYear.SelectedValue = dateOfBirth.Year.ToString();

                            ddlYear_SelectedIndexChanged(ddlYear, EventArgs.Empty);

                            ddlMonth.SelectedValue = dateOfBirth.Month.ToString();

                            ddlMonth_SelectedIndexChanged(ddlMonth, EventArgs.Empty);

                            ddlDay.SelectedValue = dateOfBirth.Day.ToString();

                            txtStreetAdress.Text = street;

                            txtState.Text = state;

                            txtCity.Text = city;

                            txtPhoneNumber.Text = phone;

                            txtEmail.Text = email;

                            txtDescription.Text = description;

                            if (gender)
                            {
                                rdbFemale.Checked = true;
                            }
                        }
                        else
                        {
                            Response.Redirect("~/ErrorPage.aspx");
                        }
                    }
                }
                else
                {
                    Response.Redirect("~/LoginMessage.aspx?id=1");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx?id=2");
            }
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
            GenerateDates gd = new GenerateDates();

            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

            // Lấy toàn bộ năm đổ vào ddlDay
            ddlDay.DataSource = gd.generateDay(year, month);
            ddlDay.DataTextField = "Text";
            ddlDay.DataValueField = "Value";
            ddlDay.DataBind(); 
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
            GenerateDates gd = new GenerateDates();

            // Lấy 12 tháng đổ vào ddlMonth
            ddlMonth.DataSource = gd.generateMonth();
            ddlMonth.DataTextField = "Text";
            ddlMonth.DataValueField = "Value";
            ddlMonth.DataBind();

            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

            // Lấy toàn bộ năm đổ vào ddlDay
            ddlDay.DataSource = gd.generateDay(year, month);
            ddlDay.DataTextField = "Text";
            ddlDay.DataValueField = "Value";
            ddlDay.DataBind(); 
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 15)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtStreetAdress.Text = "";
        txtState.Text = "";
        txtCity.Text = "";
        txtPhoneNumber.Text = "";
        txtEmail.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        int id = Convert.ToInt32(Request.QueryString["id"].ToString());

        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;
        string position = ddlPosition.SelectedValue.ToString();
        int department = Convert.ToInt32(ddlDepartment.SelectedValue.ToString());
        string street = txtStreetAdress.Text;
        string state = txtState.Text;
        string city = txtCity.Text;
        string phoneNumber = txtPhoneNumber.Text;
        string email = txtEmail.Text;
        string description = txtDescription.Text;
        string filePath = Session["fileUpload"].ToString();
        int gender = 0;

        if (rdbFemale.Checked)
        {
            gender = 1;
        }

        GenerateDates gd = new GenerateDates();

        int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
        int day = Convert.ToInt32(ddlDay.SelectedValue.ToString());
        int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());

        DateTime dateOfBirth = new DateTime(year, month, day);

        if (!email.Equals(Session["email"].ToString()))
        {
            if (txtPhoneNumber.Text.Length <= 15 && udm.checkEmail(udm.createConnection(), email))
            {
                if (fuImage.HasFile)
                {
                    CheckValid cv = new CheckValid();

                    if (cv.CheckFileType(fuImage.FileName))
                    {
                        filePath = "~/Image/" + fuImage.FileName;

                        if (System.IO.File.Exists(MapPath(filePath)))
                        {
                            int counter = 2;

                            string tempFileName = "";

                            while (System.IO.File.Exists(MapPath(filePath)))
                            {
                                tempFileName = counter.ToString() + fuImage.FileName;

                                filePath = "~/Image/" + tempFileName;

                                counter++;
                            }
                        }

                        fuImage.SaveAs(MapPath(filePath));
                    }
                }

                if (!udm.editEmployee(udm.createConnection(), id, firstName, lastName, position, department, gender, dateOfBirth, street, state, city, phoneNumber, email, filePath, description))
                {
                    Response.Redirect("MaintainMessage.aspx?ID=17");
                }
                else
                {
                    Response.Redirect("MaintainMessage.aspx?ID=16");
                }
            }
        }
        else {
            if (txtPhoneNumber.Text.Length <= 15)
            {
                if (fuImage.HasFile)
                {
                    CheckValid cv = new CheckValid();

                    if (cv.CheckFileType(fuImage.FileName))
                    {
                        filePath = "~/Image/" + fuImage.FileName;

                        if (System.IO.File.Exists(MapPath(filePath)))
                        {
                            int counter = 2;

                            string tempFileName = "";

                            while (System.IO.File.Exists(MapPath(filePath)))
                            {
                                tempFileName = counter.ToString() + fuImage.FileName;

                                filePath = "~/Image/" + tempFileName;

                                counter++;
                            }
                        }

                        fuImage.SaveAs(MapPath(filePath));
                    }
                }

                if (!udm.editEmployee(udm.createConnection(),id, firstName, lastName, position, department,gender, dateOfBirth, street, state, city, phoneNumber, email, filePath, description))
                {
                    Response.Redirect("MaintainMessage.aspx?ID=17");
                }
                else
                {
                    Response.Redirect("MaintainMessage.aspx?ID=16");
                }
            }
        }
    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        string email = txtEmail.Text;

        if (!email.Equals(Session["email"].ToString()))
        {
            if (udm.checkEmail(udm.createConnection(), email))
                args.IsValid = true;
            else
                args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
}